import dayjs from 'dayjs';
import { formatDecimal } from './util';

export const formatOptions = (options: any) => {
  if (typeof options.showLegend === 'undefined') {
    options.showLegend = true;
  }
  if (typeof options.legendPosition === 'undefined') {
    options.legendPosition = 'top';
  }
  if (typeof options.showLabel === 'undefined') {
    options.showLabel = true;
  }
  if (typeof options.labelLimit === 'undefined') {
    options.labelLimit = 0;
  }
  if (typeof options.showValue === 'undefined') {
    options.showValue = false;
  }
  if (typeof options.valuePosition === 'undefined') {
    options.valuePosition = 'outside';
  }
  if (typeof options.percentPrecision === 'undefined') {
    options.percentPrecision = 1;
  }
  if (typeof options.showSeriesName === 'undefined') {
    options.showSeriesName = false;
  }
  if (typeof options.radius === 'undefined') {
    options.radius = ['48px', '69px'];
  }
  if (typeof options.center === 'undefined') {
    options.center = ['50%', '50%'];
  }
  return options;
};

export const formatYLabelToNumber = (val: any) => {
  let num = val;
  let unit = '';
  if (val >= 100000000) {
    num = parseFloat((val / 100000000).toFixed(1));
    unit = '亿';
  } else if (val >= 1000000) {
    num = parseFloat((val / 10000).toFixed(0));
    unit = '万';
  } else if (val >= 10000) {
    num = parseFloat((val / 10000).toFixed(1));
    unit = '万';
  } else {
    num = parseFloat((val).toFixed(2));
  }
  return `{a|${num}}${unit}`;
};

export const formatYLabelToPercent = (val: any) => {
  let num;
  if (val >= 0.01) {
    num = formatDecimal(val, 1);
  } else {
    num = formatDecimal(val, 2);
  }
  const unit = '%';
  return `{a|${num}}${unit}`;
};

export const formatSeriesLabelToNumber = (val: any) => {
  return `{a|${val.toLocaleString()}}`;
};

export const formatSeriesLabelToPercent = (val: any, precision: number) => {
  const num = formatDecimal(val, precision);
  const unit = '%';
  return `{a|${num}}${unit}`;
};

export const hexToRgb = (hex: string) => {
  if (!/(^#([a-fA-F0-9]{3})$)|(^#([a-fA-F0-9]{6})$)/g.test(hex)) return null;
  const allNumberStr = '0123456789abcdef'; // 十六进制的所有数字
  const len = hex.slice(1).length;
  const str = len === 6 ? hex.slice(1) : hex.slice(1)[0].repeat(2) + hex.slice(1)[1].repeat(2) + hex.slice(1)[2].repeat(2);
  const arrStr = str.split('');
  const newArrStr = arrStr.map((item: any) => {
    return allNumberStr.indexOf((item + '').toLowerCase());
  });
  const num1 = newArrStr[0] * 16 + newArrStr[1];
  const num2 = newArrStr[2] * 16 + newArrStr[3];
  const num3 = newArrStr[4] * 16 + newArrStr[5];
  return `rgb(${num1}, ${num2}, ${num3})`;
};

export const colorToRgba = (color: string, opacity: number) => {
  let str = color;
  if (color.includes('#')) {
    str = hexToRgb(color) ?? '';
  }
  const arr = str.split(',');
  const num1 = arr[0].replace('rgb(', '');
  const num2 = arr[1];
  const num3 = arr[2].replace(')', '');
  return `rgba(${num1}, ${num2}, ${num3}, ${opacity})`;
};

export const legendStyle = (options = {}) => {
  return Object.assign({}, {
    color: '#69718C',
    fontSize: 12,
    lineHeight: 20,
    fontWeight: 'normal',
    fontFamily: 'PingFang SC, sans-serif',
    rich: {
      a: {
        verticalAlign: 'middle'
      }
    }
  }, options);
};

export const xLabelStyle = (xType: any, options: any = {}) => {
  const ret: any = {
    show: options.showLabel === true,
    // width: 40,
    hideOverlap: true, // 是否隐藏重叠的坐标轴标签
    onZero: true,
    color: '#69718C',
    fontSize: xType === 'normal' ? 12 : 14,
    lineHeight: xType === 'normal' ? 18 : 20,
    fontWeight: 'normal',
    fontFamily: xType === 'normal' ? 'PingFang SC, sans-serif' : 'number, sans-serif',
    formatter: (val: any) => {
      if (xType === 'date') {
        return dayjs.unix(val).format('MM/DD');
      } else if (xType === 'time') {
        return dayjs.unix(val).format('HH:mm');
      } else if (xType === 'normal') {
        if (options.labelLimit) {
          if (val.length > options.labelLimit) {
            return val.slice(0, options.labelLimit) + '...';
          }
        }
        if (val === '付费') {
          return `${val}{help|}`;
        }
      }
      return ' ' + val + ' ';
    },
    rich: {
      help: {
        width: 12,
        height: 12,
        margin: [0, 0, 0, 12],
        align: 'center',
        backgroundColor: {
          // eslint-disable-next-line max-len
          image: ''
        }
      }
    }
    // interval: 0, // 强制显示所有标签
  };

  if (xType === 'normal') {
    ret.interval = 0;
  }

  return ret;
};

export const yLabelStyle = (yType: any, options: any = {}, align = 'left') => {
  return {
    show: true,
    // margin: align === 'left' ? options.distance : 30,
    margin: 40,
    align,
    onZero: true,
    color: '#8F98B2',
    fontSize: 12,
    lineHeight: 18,
    fontWeight: 'normal',
    fontFamily: 'PingFang SC, sans-serif',
    formatter: (val: any) => {
      if (yType === 'number') {
        return formatYLabelToNumber(val);
      } else if (yType === 'percent') {
        return formatYLabelToPercent(val);
      }
    },
    rich: {
      a: {
        fontSize: 14,
        lineHeight: 18,
        fontFamily: 'number, sans-serif'
      }
    }
  };
};
